import { Chart, Coordinate, Interval, Legend } from 'bizcharts';
import React, { Component } from 'react';

import Debounce from 'lodash.debounce';
import autoHeight from '../utils/autoHeight';
import styles from './style.less';

export interface BarProps {
  title: React.ReactNode;
  color?: string;
  padding?: [number, number, number, number];
  height?: number;
  data: {
    x: string;
    y: number;
  }[];
  forceFit?: boolean;
  autoLabel?: boolean;
  style?: React.CSSProperties;
}

class Bar extends Component<
  BarProps,
  {
    autoHideXLabels: boolean;
  }
> {
  state = {
    autoHideXLabels: false,
  };

  root: HTMLDivElement | undefined = undefined;

  node: HTMLDivElement | undefined = undefined;

  resize = Debounce(() => {
    if (!this.node || !this.node.parentNode) {
      return;
    }
    const canvasWidth = (this.node.parentNode as HTMLDivElement).clientWidth;
    const { data = [], autoLabel = true } = this.props;
    if (!autoLabel) {
      return;
    }
    const minWidth = data.length * 30;
    const { autoHideXLabels } = this.state;

    if (canvasWidth <= minWidth) {
      if (!autoHideXLabels) {
        this.setState({
          autoHideXLabels: true,
        });
      }
    } else if (autoHideXLabels) {
      this.setState({
        autoHideXLabels: false,
      });
    }
  }, 500);

  componentDidMount() {
    window.addEventListener('resize', this.resize, { passive: true });
  }

  componentWillUnmount() {
    window.removeEventListener('resize', this.resize);
  }

  handleRoot = (n: HTMLDivElement) => {
    this.root = n;
  };

  handleRef = (n: HTMLDivElement) => {
    this.node = n;
  };

  render() {
    const {
      height = 1,
      title,
      forceFit = true,
      data,
      padding,
    } = this.props;


    const scale = {
      x: {
        type: 'cat',
      },
      y: {
        min: 0,
      },
    };

    // const checkedLegends = ['不良品计数', '未完成数'];
    const colors = ['#4F7DE7', '#37C377'];

    return (
      <div className={styles.chart} style={{ height }} ref={this.handleRoot}>
        <div ref={this.handleRef}>
          {title && <h4 style={{ marginBottom: 20 }}>{title}</h4>}
          <Chart
            scale={scale}
            height={title ? height - 41 : height}
            forceFit={forceFit}
            data={data}
            padding={padding || 'auto'}
          >
            <Legend
              custom
              allowAllCanceled
              items={[
                {
                  value: '不良品计数',
                  name: '不良品计数',
                  marker: {
                    symbol: 'square',
                    style: { fill: colors[0] },
                  },
                },
                {
                  value: '未完成数',
                  name: '未完成数',
                  marker: {
                    symbol: 'square',
                    style: { stroke: colors[1] },
                  },
                },
              ]}
            />
            <Coordinate transpose />
            <Interval
              adjust={[
                {
                  type: 'stack',
                },
              ]}
              color="name"
              position="名称*数值"
            />
          </Chart>
        </div>
      </div>
    );
  }
}

export default autoHeight()(Bar);
